<?php
/*
	//include [require] mysql dump engine
	//$url = base_url().'asset/admin/js/MySQLDump.class.php';
	//echo $url;
	//require_once(APPPATH.'asset/admin/js/MySQLDump.class.php');
	//require_once($url);
	 require_once APPPATH.'libraries/MySQL_export.php';
	//mysql info
	$dbhost = ''; //db host
	$dbuser = ''; //db user name
	$dbpass = ''; //db password
	$dbname = 'bp3ti'; //db to work with
	
	$drop_table_if_exists = true; //should we drop table if exist?
	
	// no need for editing further
	
	$backup = new MySQLDump(); //create new instance of MySQLDump
	
	$backup->droptableifexists = $drop_table_if_exists; //set drop table if exists
	
	//$backup->connect($dbhost,$dbuser,$dbpass,$dbname); //connect
	//if (!$backup->connected) { die('Error: '.$backup->mysql_error); } //if not connected, display error
	$backup->list_tables(); //list all tables
	$broj = count($backup->tables); //count all tables, $backup->tables will be array of table names
	
			$tz_string = "Asia/Jakarta"; // Use one from list of TZ names http://php.net/manual/en/timezones.php 
			$tz_object = new DateTimeZone($tz_string); 
			
			$datetime = new DateTime(); 
			$datetime->setTimezone($tz_object); 
			
	header("Content-Disposition: attachment; filename=database SIMKEU BP3TI - ".$datetime->format('Y.m.d')."-".$datetime->format('H.i.s').".sql");
	header("Content-type: application/download");
	
	
	//echo "<pre>\n"; //start preformatted output
	echo "-- Dumping tables for database: $dbname\n"; //write "intro" ;)
	echo "create database if not exists bp3ti; use bp3ti;";
	for ($i=0;$i<$broj;$i++) {
		$table_name = $backup->tables[$i]; //get table name
		$backup->dump_table($table_name); //dump it to output (buffer)
		echo  htmlspecialchars($backup->output); //write output
	}
	
	echo "\n\n-- MySQLDump by husen darwis"; //write "outtro" :)
	 
	*/
?> 

 
<?php

backup_tables('localhost','root','','bp3ti');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
        
        $link = mysql_connect($host,$user,$pass);
		 $return = '';
        mysql_select_db($name,$link);
        
        //get all of the tables
        if($tables == '*')
        {
                $tables = array();
                $result = mysql_query('SHOW TABLES');
                while($row = mysql_fetch_row($result))
                {
                        $tables[] = $row[0];
                }
        }
        else
        {
                $tables = is_array($tables) ? $tables : explode(',',$tables);
        }
        
        //cycle through
        foreach($tables as $table)
        {
				 
                $result = mysql_query('SELECT * FROM '.$table);
                $num_fields = mysql_num_fields($result);
               
                $return.= 'DROP TABLE IF EXISTS '.$table.';';
                $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
                $return.= "\n\n".$row2[1].";\n\n";
                
                for ($i = 0; $i < $num_fields; $i++) 
                {
                        while($row = mysql_fetch_row($result))
                        {
                                $return.= 'INSERT INTO '.$table.' VALUES(';
                                for($j=0; $j<$num_fields; $j++) 
                                {
                                        $row[$j] = addslashes($row[$j]);
                                        $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                                        if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                                        if ($j<($num_fields-1)) { $return.= ','; }
                                }
                                $return.= ");\n";
                        }
                }
                $return.="\n\n\n";
        }
        
        //save file
		
		//echo $return;
       // $handle = fopen('db-backup2-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
       // fwrite($handle,$return);
       // fclose($handle);
	
	header("Content-Disposition: attachment; filename=database SIMKEU BP3TI - ".$datetime->format('Y.m.d')."-".$datetime->format('H.i.s').".sql");
	header("Content-type: application/download");
	
		
		   echo $return;
}

?> 
